package net.funwoo.pandago.ui.user;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import net.funwoo.pandago.AppConfig;
import net.funwoo.pandago.R;
import net.funwoo.pandago.ui.BaseFragment;
import net.funwoo.pandago.util.StringUtils;

import cn.smssdk.SMSSDK;

/**
 * Created by Bob Peng on 2015/5/7.
 */
public class PhoneVerifyFragment extends BaseFragment implements View.OnClickListener {
    private static Handler mHandler;
    private EditText mPhoneText;
    private EditText mVerifyText;
    private Button mVerifyBtn;
    private Button mNextBtn;
    private String mDelayText;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_phone_verify, container, false);
        mPhoneText = (EditText) view.findViewById(R.id.register_phone);
        mVerifyText = (EditText) view.findViewById(R.id.register_verify_code);
        mVerifyBtn = (Button) view.findViewById(R.id.register_verify_btn);
        mNextBtn = (Button) view.findViewById(R.id.register_next_btn);

        mVerifyText.addTextChangedListener(new VerifyCodeChangeListener());
        mNextBtn.setOnClickListener(this);
        mVerifyBtn.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.register_next_btn) {
            onNextBtnClick();
        } else if (id == R.id.register_verify_btn) {
            onVerifyCodeRequest();
        }
    }

    private void onNextBtnClick() {
        if (checkField()) {
            Intent intent = new Intent(getContext(), RegisterActivity.class);
            intent.putExtra(AppConfig.KEY_USER_ID, mPhoneText.getText().toString());
            startActivity(intent);
        }
    }

    private boolean checkField() {
        String phone = mPhoneText.getText().toString();
        String code = mVerifyText.getText().toString();

        // Empty check
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)) {
            Toast.makeText(mContext, R.string.msg_field_empty, Toast.LENGTH_SHORT).show();
            return false;
        }

        // check phone format
        if (!StringUtils.isPhoneText(phone)) {
            Toast.makeText(getContext(), R.string.msg_invalid_phone_number, Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }

    private void onVerifyCodeRequest() {
        mDelayText = getString(R.string.btn_verify_delay);
        mVerifyBtn.setEnabled(false);
        mVerifyBtn.setText("60 " + mDelayText);

        if (mPhoneText.getText() == null || !StringUtils.isPhoneText(mPhoneText.getText().toString())) {
            Toast.makeText(getContext(), R.string.msg_invalid_phone_number, Toast.LENGTH_SHORT).show();
            return;
        }

        SMSSDK.getVerificationCode("86", mPhoneText.getText().toString());

        Runnable timer = new Runnable() {
            int second = 60;

            @Override
            public void run() {
                if (second > 0) {
                    second--;
                    mVerifyBtn.setText(second + " " + mDelayText);
                    mHandler.postDelayed(this, 1000);
                } else {
                    mVerifyBtn.setEnabled(true);
                    mVerifyBtn.setText(R.string.btn_confirm_code);
                }
            }
        };
        if (mHandler == null) {
            mHandler = new Handler();
        }
        mHandler.postDelayed(timer, 1000);
    }

    private class VerifyCodeChangeListener implements TextWatcher {
        private boolean mIsAvailable = false;

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            boolean available = s.length() > 0 && mPhoneText.getText().length() > 0;
            if (available != mIsAvailable) {
                mNextBtn.setEnabled(available);
                mIsAvailable = available;
            }
        }
    }
}
